package atom.core3.security.help;

//客户端证书的验证

/**
 * 
 * <p>
 * Title:
 * </p>
 * <p>
 * 由CA中心验证签发的客户端证书是否合法:
 * </p>
 * <p>
 * Copyright: Copyright (c) 2003
 * </p>
 * <p>
 * Company:
 * </p>
 * 
 * @author unascribed
 * @version 1.0
 */
public class CheckCertSign
{
	public CheckCertSign()
	{
	}

	public static void main(String[] args)
	{
		//String cacert = "d:/rootCA.cer";
		//String usercert = "D:/client.cer";
		CheckCertSign checkCertSign1 = new CheckCertSign();
		//checkCertSign1.checkSign(cacert,usercert);
		String cacert= "d:/rootCA.pfx";
		String usercert = "D:/client.pfx";
		checkCertSign1.checkPfkSign(cacert,"111111",usercert,"111111");
	}

	public boolean checkSign(String cacert,String usercert)
	{
		try
		{
			CertInfo caCertInfo = new CertInfo();
			caCertInfo.ReadPublicKeyFromX509Certificate(cacert);
			// 用户的签名证书
			CertInfo userCertInfo = new CertInfo();
			userCertInfo.ReadPublicKeyFromX509Certificate(usercert);
			
			boolean pass = false;
			try
			{
				userCertInfo.getCer().verify(caCertInfo.getPubKey());
				pass = true;
			}
			catch (Exception e)
			{
				pass = false;
				System.out.println(e);
			}
			if (pass)
			{
				System.out.println("验证结果正确！");
			}
			else
			{
				System.out.println("验证结果失败！");
			}
			return true;
		}
		catch (Exception ex)
		{
			System.out.println("验证结果失败！");
			return false;
		}

	}

	public boolean checkPfkSign(String cacert,String cacertPassword,String usercert,String usercertPassword)
	{
		CertInfo caCertInfo = new CertInfo();
		// 用户的签名证书
		CertInfo userCertInfo = new CertInfo();		
		try
		{
			caCertInfo.ReadKeyFromPKCS12(cacert, cacertPassword);

			userCertInfo.ReadKeyFromPKCS12(usercert, usercertPassword);
			boolean pass = false;
			try
			{
				userCertInfo.getCer().verify(caCertInfo.getPubKey());
				pass = true;
			}
			catch (Exception e)
			{
				pass = false;
				System.out.println(e);
			}
			if (pass)
			{
				System.out.println("验证结果正确！");
			}
			else
			{
				System.out.println("验证结果失败！");
			}
			return true;
		}
		catch (Exception ex)
		{
			System.out.println("验证结果失败！");
			return false;
		}

	}

}
